<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Article;
use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     * | GET|HEAD| Admin/article | Admin.article.index
     * @return \Illuminate\Http\Response
     *  显示文章类表
     */
    public function index()
    {
        $data = Article::orderBy('art_id','desc')->paginate(10);
        return view('Admin.article.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     * | GET|HEAD| Admin/article/create | Admin.article.create
     * @return \Illuminate\Http\Response
     * 显示文章添加页面
     */
    public function create()
    {
        $cate = (new Category)->tree();
        return view('Admin.article.add',compact('cate'));
    }

    /**
     * Store a newly created resource in storage.
     * | POST| Admin/articleAdmin/article| Admin.article.store|
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 执行文章添加
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');
        $rules = [
            'art_title'=>'required',
            'art_content'=>'required',
        ];
        $message = [
            'art_title.required'=>'文章标题不能为空',
            'art_content.required'=>'文章标题不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator -> passes()) {
            $input['art_time'] = time();
            $res = Article::create($input);
            if ($res) {
                return redirect('Admin/article')->with('info', '文章添加成功');
            } else {
                return back()->with('info', '数据添加失败，请稍后再试');
            }
        }else
        {
            return back()->withErrors($validator);
        }
    }

    /**
     * 图片上传
     */
    public function upload()
    {
        $file = Input::file('Filedata');
        if($file -> isValid())
        {
            $entension = $file -> getClientOriginalExtension();//获取上传文件后缀
            $newName = md5(time().mt_rand(100,999).uniqid()).".".$entension;
            $path = $file -> move(base_path().'/public/uploads',$newName);
            return '/uploads/'.$newName;
        }else
        {
            return 1;
        }
    }

    /**
     * Show the form for editing the specified resource.
     * | GET|HEAD| Admin/article/{article}/edit| Admin.article.edit
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 显示文章修改页面
     */
    public function edit($id)
    {
        $data = Article::find($id);
        $cate = (new Category)->tree();
        return view('Admin.article.edit',compact('data','cate'));
    }

    /**
     * Update the specified resource in storage.
     * | PUT|PATCH | Admin/article/{article} | Admin.article.update
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章更新
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token','_method');
        $info = Article::find($id);
        if($info['art_img'] != null)
        {
            unlink(base_path().'/public'.$info['art_img']);
        }
        $res = Article::where('art_id',$id)->update($input);
        if($res)
        {
            return redirect('Admin/article')->with('info','分类更新成功');
        }else
        {
            return back()->with('info','分类更新失败，请稍候再试');
        }
    }

    /**
     * Remove the specified resource from storage.
     * | DELETE | Admin/article/{article} | Admin.article.destroy
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章删除
     */
    public function destroy($id)
    {
        $info = Article::find($id);
        if($info['art_img'] != null)
        {
            $re = unlink(base_path().'/public'.$info['art_img']);
        }
        $res = Article::where('art_id',$id)->delete();
        if($res ||($res && $re))
        {
            $data = [
                'status'=>0,
                'msg'=>'分类删除成功',
            ];
        }else
        {
            $data = [
                'status'=>1,
                'msg'=>'分类删除失败，请稍候再试',
            ];
        }
        return $data;
    }

    public function show($id)
    {
        //
    }
}
